from http.client import responses

import requests

from config import BASE_URL


class OrderBusiness:
    # 登录
    def login(self, username, password):
        url = BASE_URL+"/wx/auth/login"
        login_data = {"username": username, "password": password}
        response = requests.post(url=url, json=login_data)
        return response

    # 搜索
    def search(self, keyword):
        url = BASE_URL+"/wx/goods/list"
        response = requests.get(url=url,params={"keyword":keyword,"page":1,"limit":10,"categoryId":0})
        return response

    # 购物车
    def cart(self, goodsId, number, productId, token):
        url = BASE_URL+"/wx/cart/add"
        cart_data = {"goodsId":goodsId,"number":number,"productId":productId}
        response = requests.post(url=url,json=cart_data,headers={"X-Litemall-Token":token})
        return response

    # 提交订单
    def submit_order(self, addressId, cartId, couponId, userCouponId, grouponLinkId, grouponRulesId, token, message=""):
        url = BASE_URL+"/wx/order/submit"
        order_data = {"addressId":addressId,"cartId":cartId,"couponId":couponId,"userCouponId":userCouponId,"grouponLinkId":grouponLinkId,"grouponRulesId":grouponRulesId,"message":message}
        response = requests.post(url=url,json=order_data,headers={"X-Litemall-Token":token})
        return response

    # 查看订单
    def order_list(self, token):
        url = BASE_URL+"/wx/order/list"
        response = requests.get(url=url,headers={"X-Litemall-Token":token},params={"showType":0,"page":1,"limit":10})
        return response

if __name__ == '__main__':
    ob = OrderBusiness()
    # 登录
    response = ob.login("user123", "user123")
    # print(response.json())
    # print(response.json().get("data").get("token"))
    token = response.json().get("data").get("token")
    # print(response.json())
    # 搜索
    # print(ob.search("母亲节").json())
    # print(ob.search("母亲节").json().get("data").get("list")[0].get("id"))
    # goodsId = ob.search("母亲节").json().get("data").get("list")[0].get("id")
    # 购物车
    # response = ob.cart(goodsId,5,2,token)
    # print(response.json())
    # 提交订单
    # response = ob.submit_order(1,0,0,0,0,0,token)
    # print(response.json())
    # 查看订单
    print(ob.order_list(token).json())




